package sc.server.world;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import sc.datainterchange.CodedOutputBuffer;
import sc.server.Database;

/**
 * Avatar
 * 
 * @author stephencarmody@gmail.com Stephen Carmody
 */
public class Avatar extends Container
{
	// Avatar object type identity in data interchange
	private static final int OBJECTTYPEID = 1;

	// Map of known Avatars
	private static Map<String, Avatar> avatars = new HashMap<String, Avatar>();
            
	// Avatars attributes
	private String name;
	private int height;

	/**
	 * Creates an instance of Player.
	 * 
	 * @param id
	 *             Object id
	 * @throws SQLException 
	 */
	private Avatar(int id) throws SQLException
	{
		super(id);
	}

	@Override
	public void write(CodedOutputBuffer codedoutput) throws IOException
	{
		super.write(codedoutput);
		codedoutput.writeRawVarint32(this.height);
	}

	@Override
	public int getObjectTypeID()
	{
		return Avatar.OBJECTTYPEID;
	}
	
	/**
	 * Sets the name.
	 * 
	 * @param name
	 * 			New name
	 */
	public void setName(String name)
	{
	    if (avatars.containsKey(this.name))
            avatars.remove(this.name);
        this.name = name;
	    avatars.put(name, this);
	}
	
	/**
	 * Returns the name.
	 * 
	 * @return name
	 */
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * Sets the height.
	 * 
	 * @param height
	 * 			New height
	 */
	public void setHeight(int height)
	{
		this.height = height;
	}
	
	/**
	 * Returns the height.
	 * 
	 * @return height
	 */
	public int getHeight()
	{
		return this.height;
	}
	
    /**
     * Reads all known Tiles from the database.
     * 
     * @throws SQLException
     */
    public static void initialise() throws SQLException
    {
        PreparedStatement readStmt = Database.getInstance().getConnection()
                .prepareStatement("select a.*, c.* from avatar a, container c where a.id = c.id");
        ResultSet rs = readStmt.executeQuery();
        Avatar avatar;

        while (rs.next())
        {
            avatar = new Avatar(rs.getInt(1));
            avatar.setParent(rs.getInt(3));
            avatar.setX(rs.getInt(4));
            avatar.setY(rs.getInt(5));
            avatar.setZ(rs.getInt(6));
            avatar.setName(rs.getString(8));
            avatar.setHeight(rs.getInt(9));
        }
    }
    
    /**
     * Gets an Avatar instance.
     * 
     * @param name
     *            Unique id
     * 
     * @return an Avatar instance
     * @throws SQLException 
     */
    public static Avatar get(String name) throws SQLException
    {
        // Do we already have an Avatar instance?
        if (avatars.containsKey(name))
            return avatars.get(name);

        // No, so create one and add it to the map
        Avatar avatar = new Avatar(Container.create(Avatar.OBJECTTYPEID));
        avatar.setName(name);
        avatars.put(name, avatar);
        return avatar;
    }
}
